package com.novabluetooth.data.ecgxmlparse;

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import com.novabluetooth.bean.ECG;

import android.util.Log;
import android.util.Xml;

public class PullECGParser implements ECGParser {
	@Override
	public ECG parse(InputStream is) throws Exception {
		ECG ecg = null;

		// XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		// XmlPullParser parser = factory.newPullParser();

		XmlPullParser parser = Xml.newPullParser(); // 由android.util.Xml创建一个XmlPullParser实例
		parser.setInput(is, "UTF-8"); // 设置输入流 并指明编码方式

		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				ecg = new ECG();
				break;
			case XmlPullParser.START_TAG:
				Log.w("eeeeeepull解析------", "ee" + parser.getName());

				if (parser.getName().equals("ID")) {
					eventType = parser.next();
					ecg.setIdCard(parser.getText());

				} else if (parser.getName().equals("Parameter")) {

					// <Parameter AnalysisState="1" PWidth="46"
					// PExist="1" RRInterval="0" HeartRate="84"
					// PRInterval="82" QRSDuration="47" QTD="174"
					// QTC="205" PAxis="50" QRSAxis="44" TAxis="51"
					// RV5V6="0.000" SV1V2="0.000" RV1SV5="0.000"
					// RV5SV1="1502.0" RV5="996.0" SV1="-505.99997">
					ecg.setpWidth(Integer.parseInt(parser.getAttributeValue(1)));
					ecg.setHeartRate(Integer.parseInt(parser.getAttributeValue(4)));
					ecg.setpRInterval(Integer.parseInt(parser.getAttributeValue(5)) * 2);
					ecg.setqRSDuration(Integer.parseInt(parser.getAttributeValue(6)) * 2);
					ecg.setqTD(Integer.parseInt(parser.getAttributeValue(7)) * 2);
					ecg.setqTC(Integer.parseInt(parser.getAttributeValue(8)) * 2);
					ecg.setpAxis(Integer.parseInt(parser.getAttributeValue(9)));
					ecg.setqRSAxis(Integer.parseInt(parser.getAttributeValue(10)));
					ecg.settAxis(Integer.parseInt(parser.getAttributeValue(11)));
					ecg.setrV5(Float.parseFloat(parser.getAttributeValue(16)) / 1000);
					ecg.setRv5sv1(Float.parseFloat(parser.getAttributeValue(15)) / 1000);
					ecg.setsV1(Float.parseFloat(parser.getAttributeValue(17)) / 1000);
					eventType = parser.next();
				} else if (parser.getName().equals("ResCode")) {
					
					/*9110 正常心电图
					9120 非典型心电图
					9130 可疑心电图
					9140 异常心电图*/
					// <ResCode Code1="9110">窦性心律</ResCode> 
					// <ResCode Code1="0"/>      无诊断结果
					String resultCode=parser.getAttributeValue(0);
					
					String pre_reuslt="";
					if(resultCode.equals("0")){
						pre_reuslt="无心电图诊断";
						ecg.setDiagnoseResult(pre_reuslt);
					}else{
						 if(resultCode.equals("9110")){
								pre_reuslt="**正常心电图**";
							}else if(resultCode.equals("9120")){
								pre_reuslt="**非典型心电图**";
							} else if(resultCode.equals("9130")){
								pre_reuslt="**可疑心电图**";
							} else if(resultCode.equals("9140")){
								pre_reuslt="**异常心电图**";
							} 
							eventType = parser.next();
							ecg.setDiagnoseResult(pre_reuslt+"\n"+parser.getText());

					}
				}
				break;
			case XmlPullParser.END_TAG:
				if (parser.getName().equals("ECG")) {
					Log.w("eeeeeeeeee000000", ecg.toString());

				}
				break;
			}
			eventType = parser.next();
		}
		return ecg;
	}

	@Override
	public String serialize(ECG ecg) throws Exception {

		return null;
	}

}
